<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>6-1 网页开关灯</title>
</head>
<body>
<button id="btn" onclick="Kaiguan()">开关灯</button>

<script>
    let flag = true;

    function Kaiguan() {
        const body = document.body;
        if (flag) {
            body.style.backgroundColor = "white";
        } else {
            body.style.backgroundColor = "black";
        }
        flag = !flag;
    }
</script>
</body>
</html>